package com.esri.android.map;

import android.util.Log;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.android.map.popup.FeatureTablePopupInfo;
import com.esri.android.map.popup.PopupLayer;
import com.esri.core.geodatabase.GeodatabaseFeatureServiceTable;
import com.esri.core.geodatabase.GeodatabaseFeatureTable;
import com.esri.core.geodatabase.GeopackageFeatureTable;
import com.esri.core.geodatabase.ShapefileFeatureTable;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Geometry;
import com.esri.core.map.CallbackListener;
import com.esri.core.map.Feature;
import com.esri.core.map.FeatureResult;
import com.esri.core.map.Field;
import com.esri.core.map.GraphicsUtil;
import com.esri.core.map.popup.PopupFieldInfo;
import com.esri.core.map.popup.PopupInfo;
import com.esri.core.renderer.Renderer;
import com.esri.core.table.FeatureTable;
import com.esri.core.table.TableException;
import com.esri.core.tasks.query.QueryParameters;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.Future;

/* loaded from: classes.dex */
public class FeatureLayer extends Layer implements PopupLayer {
    private static final String a = "FeatureLayer";
    private static final String b = "The layer is not initialized or initialization failed.";
    private static final String c = "The argument featureTable cannot be null.";
    private static final String d = "The associated geodatabase is not initialized yet.";
    private static final String e = "The argument oid has to be greater or equal zero.";
    private static final String f = "The argument oids cannot be null.";
    private static final String g = "FeatureTable is invalid";
    private boolean s;
    private final FeatureTable t;
    private final boolean u;
    private Renderer v;
    private Future<Void> w;
    private String x;

    /* loaded from: classes.dex */
    public enum SelectionMode {
        ADD,
        NEW,
        SUBTRACT
    }

    native long nativeCreateLayer();

    native void nativeEnableCallback(long j, boolean z);

    native void nativeEnableLabels(long j, boolean z);

    native long[] nativeGetFeaturesAt(long j, long j2, double d2, double d3, int i, int i2);

    native int nativeGetGeometryType(long j);

    native byte[] nativeGetRenderer(long j);

    native int nativeGetSelectionColor(long j);

    native long[] nativeGetSelectionIDs(long j);

    native boolean nativeInitializeFromGDB(long j, long j2, int i);

    native boolean nativeInitializeFromGpkg(long j, long j2, String str);

    native boolean nativeIsFeatureSelected(long j, long j2);

    native boolean nativeRemoveAllSelection(long j);

    native void nativeSetDefinitionExpression(long j, String str);

    native void nativeSetFeaturesVisible(long j, long[] jArr, boolean z);

    native boolean nativeSetRenderer(long j, String str);

    native void nativeSetSelectionColor(long j, int i);

    native boolean nativeSetSelectionIDs(long j, long[] jArr, boolean z);

    native boolean nativeUnselectFeature(long j, long j2);

    native boolean nativeUnselectFeatures(long j, long[] jArr);

    public FeatureLayer(FeatureTable featureTable) {
        super(false);
        this.s = false;
        this.w = null;
        if (featureTable == null) {
            throw new IllegalArgumentException(c);
        }
        this.t = featureTable;
        if (featureTable instanceof GeodatabaseFeatureTable) {
            this.u = true;
            a((GeodatabaseFeatureTable) featureTable);
        } else {
            this.u = false;
            initLayer();
        }
        HashMap hashMap = new HashMap();
        hashMap.put(0, createPopupInfo());
        setPopupInfos(hashMap);
    }

    public FeatureTablePopupInfo createPopupInfo() {
        PopupInfo popupInfo = new PopupInfo();
        popupInfo.setMaxScale(getMaxScale());
        popupInfo.setMinScale(getMinScale());
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        for (Field field : this.t.getFields()) {
            PopupFieldInfo popupFieldInfo = new PopupFieldInfo();
            popupFieldInfo.setFieldName(field.getName());
            popupFieldInfo.setLabel(field.getAlias());
            if (field.getFieldType() == 100 || field.getFieldType() == 80) {
                popupFieldInfo.setEditable(false);
                popupFieldInfo.setVisible(false);
            } else {
                popupFieldInfo.setEditable(field.isEditable());
            }
            linkedHashMap.put(field.getName(), popupFieldInfo);
        }
        popupInfo.setFieldInfos(linkedHashMap);
        popupInfo.setTitle(this.t.getTableName());
        FeatureTable featureTable = this.t;
        if (featureTable instanceof GeodatabaseFeatureTable) {
            popupInfo.setShowAttachments(((GeodatabaseFeatureTable) featureTable).hasAttachments());
        }
        FeatureTablePopupInfo featureTablePopupInfo = new FeatureTablePopupInfo(popupInfo, getFeatureTable());
        FeatureTable featureTable2 = this.t;
        if (featureTable2 instanceof GeodatabaseFeatureTable) {
            featureTablePopupInfo.setLayer(((GeodatabaseFeatureTable) featureTable2).getLayerServiceInfo());
        }
        return featureTablePopupInfo;
    }

    private void a(GeodatabaseFeatureTable geodatabaseFeatureTable) {
        if (geodatabaseFeatureTable.getGeodatabase() == null || geodatabaseFeatureTable.getGeodatabase().getHandle() == 0) {
            throw new IllegalStateException(d);
        }
        if (geodatabaseFeatureTable instanceof GeodatabaseFeatureServiceTable) {
            setUrl(((GeodatabaseFeatureServiceTable) geodatabaseFeatureTable).getServiceUrl());
        } else {
            setUrl(geodatabaseFeatureTable.getGeodatabase().getPath());
        }
        initLayer();
    }

    @Override // com.esri.android.map.Layer
    protected void initLayer() {
        if (this.nativeHandle == 0) {
            this.nativeHandle = create();
        }
        if (this.nativeHandle == 0) {
            changeStatus(OnStatusChangedListener.STATUS.fromInt(-1000));
        }
        boolean z = false;
        try {
            if (this.u) {
                z = b((GeodatabaseFeatureTable) this.t);
            } else {
                FeatureTable featureTable = this.t;
                if (featureTable instanceof GeopackageFeatureTable) {
                    z = a((GeopackageFeatureTable) featureTable);
                } else if (featureTable instanceof ShapefileFeatureTable) {
                    z = a((ShapefileFeatureTable) featureTable);
                }
            }
            if (z) {
                Envelope envelope = new Envelope();
                Envelope extent = this.t.getExtent();
                if (extent != null) {
                    extent.queryEnvelope(envelope);
                    setFullExtent(envelope);
                }
                setDefaultSpatialReference(this.t.getSpatialReference());
                setName(this.t.getTableName());
                c();
                changeStatus(OnStatusChangedListener.STATUS.INITIALIZED);
                return;
            }
            Log.e(com.esri.core.internal.a.a, "url =" + getUrl() + " is invalid path.");
            changeStatus(OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.EsriStatusException.INIT_FAILED_ARCGIS_FEATURE_LAYER));
        } catch (Exception e2) {
            Log.e(com.esri.core.internal.a.a, "url =" + getUrl(), e2);
            changeStatus(OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.EsriStatusException.INIT_FAILED_ARCGIS_FEATURE_LAYER));
        }
    }

    private boolean b(GeodatabaseFeatureTable geodatabaseFeatureTable) {
        return nativeInitializeFromGDB(this.nativeHandle, geodatabaseFeatureTable.getGeodatabase().getHandle(), geodatabaseFeatureTable.getFeatureServiceLayerId());
    }

    private boolean a(GeopackageFeatureTable geopackageFeatureTable) {
        if (geopackageFeatureTable.getGeopackage() == null || geopackageFeatureTable.getGeopackage().getHandle() == 0) {
            throw new IllegalStateException(g);
        }
        return nativeInitializeFromGpkg(this.nativeHandle, geopackageFeatureTable.getGeopackage().getHandle(), geopackageFeatureTable.getTableName());
    }

    private boolean a(ShapefileFeatureTable shapefileFeatureTable) {
        if (shapefileFeatureTable.getHandle() == 0) {
            throw new IllegalStateException(g);
        }
        return nativeInitializeFromGDB(this.nativeHandle, shapefileFeatureTable.getHandle(), -1);
    }

    private void c() {
        FeatureTable featureTable = this.t;
        if (!(featureTable instanceof GeodatabaseFeatureServiceTable) || ((GeodatabaseFeatureServiceTable) featureTable).getFeatureRequestMode() == GeodatabaseFeatureServiceTable.FeatureRequestMode.MANUAL_CACHE) {
            return;
        }
        nativeEnableCallback(this.nativeHandle, true);
    }

    @Override // com.esri.android.map.Layer
    protected long create() {
        return nativeCreateLayer();
    }

    public Feature getFeature(long j) {
        try {
            return this.t.getFeature(j);
        } catch (TableException e2) {
            throw new RuntimeException(e2);
        }
    }

    public long[] getFeatureIDs(float f2, float f3, int i) {
        return getFeatureIDs(f2, f3, i, -1);
    }

    public long[] getFeatureIDs(float f2, float f3, int i, int i2) {
        if (this.nativeHandle == 0) {
            throw new IllegalStateException(b);
        }
        return nativeGetFeaturesAt(this.nativeHandle, 0L, f2, f3, i + 1, i2);
    }

    public void setEnableLabels(boolean z) {
        if (this.nativeHandle == 0) {
            throw new IllegalStateException(b);
        }
        this.s = z;
        nativeEnableLabels(this.nativeHandle, this.s);
    }

    public boolean isEnabledLabels() {
        return this.s;
    }

    public Geometry.Type getGeometryType() {
        if (this.nativeHandle == 0 || !isInitialized()) {
            throw new IllegalStateException(b);
        }
        int nativeGetGeometryType = nativeGetGeometryType(this.nativeHandle);
        Geometry.Type type = Geometry.Type.UNKNOWN;
        if (nativeGetGeometryType != 513) {
            if (nativeGetGeometryType != 3077) {
                if (nativeGetGeometryType != 8710) {
                    if (nativeGetGeometryType != 25607) {
                        return nativeGetGeometryType != 27656 ? type : Geometry.Type.POLYGON;
                    }
                    return Geometry.Type.POLYLINE;
                }
                return Geometry.Type.MULTIPOINT;
            }
            return Geometry.Type.ENVELOPE;
        }
        return Geometry.Type.POINT;
    }

    public void setFeatureVisible(long j, boolean z) {
        if (this.nativeHandle == 0) {
            throw new IllegalStateException(b);
        }
        if (j < 0) {
            throw new IllegalArgumentException(e);
        }
        nativeSetFeaturesVisible(this.nativeHandle, new long[]{j}, z);
    }

    public void selectFeatures(long[] jArr, boolean z) {
        if (this.nativeHandle == 0) {
            throw new IllegalStateException(b);
        }
        if (jArr == null) {
            throw new IllegalArgumentException(f);
        }
        nativeSetSelectionIDs(this.nativeHandle, jArr, z);
    }

    public void selectFeature(long j) {
        if (this.nativeHandle == 0) {
            throw new IllegalStateException(b);
        }
        selectFeatures(new long[]{j}, true);
    }

    public void unselectFeatures(FeatureResult featureResult) {
        unselectFeatures(a(featureResult));
    }

    public void unselectFeatures(long[] jArr) {
        if (this.nativeHandle == 0) {
            throw new IllegalStateException(b);
        }
        nativeUnselectFeatures(this.nativeHandle, jArr);
    }

    private static long[] a(FeatureResult featureResult) {
        ArrayList arrayList = new ArrayList();
        Iterator<Object> it = featureResult.iterator();
        while (it.hasNext()) {
            arrayList.add(Long.valueOf(((Feature) it.next()).getId()));
        }
        long[] jArr = new long[arrayList.size()];
        int i = 0;
        Iterator it2 = arrayList.iterator();
        while (it2.hasNext()) {
            jArr[i] = ((Long) it2.next()).longValue();
            i++;
        }
        return jArr;
    }

    public void unselectFeature(long j) {
        if (this.nativeHandle == 0) {
            throw new IllegalStateException(b);
        }
        nativeUnselectFeature(this.nativeHandle, j);
    }

    public void clearSelection() {
        if (this.nativeHandle == 0) {
            throw new IllegalStateException(b);
        }
        nativeRemoveAllSelection(this.nativeHandle);
    }

    /* JADX WARN: Removed duplicated region for block: B:48:0x0050 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    public com.esri.core.renderer.Renderer getRenderer() {
        /*
            r7 = this;
            r0 = 0
            long r1 = r7.nativeHandle     // Catch: java.lang.Throwable -> L3e java.lang.Exception -> L43
            r3 = 0
            int r5 = (r1 > r3 ? 1 : (r1 == r3 ? 0 : -1))
            if (r5 == 0) goto L3b
            com.esri.core.renderer.Renderer r1 = r7.v     // Catch: java.lang.Throwable -> L3e java.lang.Exception -> L43
            if (r1 != 0) goto L3b
            long r1 = r7.nativeHandle     // Catch: java.lang.Throwable -> L3e java.lang.Exception -> L43
            byte[] r1 = r7.nativeGetRenderer(r1)     // Catch: java.lang.Throwable -> L3e java.lang.Exception -> L43
            org.codehaus.jackson.JsonParser r1 = com.esri.core.internal.util.d.a(r1)     // Catch: java.lang.Throwable -> L3e java.lang.Exception -> L43
            if (r1 == 0) goto L2d
            com.esri.core.renderer.Renderer r0 = com.esri.core.internal.util.d.g(r1)     // Catch: java.lang.Exception -> L2b java.lang.Throwable -> L4d
            if (r1 == 0) goto L2a
            r1.close()     // Catch: java.io.IOException -> L23
            goto L2a
        L23:
            r0 = move-exception
            java.lang.RuntimeException r1 = new java.lang.RuntimeException
            r1.<init>(r0)
            throw r1
        L2a:
            return r0
        L2b:
            r0 = move-exception
            goto L47
        L2d:
            if (r1 == 0) goto L3a
            r1.close()     // Catch: java.io.IOException -> L33
            goto L3a
        L33:
            r0 = move-exception
            java.lang.RuntimeException r1 = new java.lang.RuntimeException
            r1.<init>(r0)
            throw r1
        L3a:
            return r0
        L3b:
            com.esri.core.renderer.Renderer r0 = r7.v     // Catch: java.lang.Throwable -> L3e java.lang.Exception -> L43
            return r0
        L3e:
            r1 = move-exception
            r6 = r1
            r1 = r0
            r0 = r6
            goto L4e
        L43:
            r1 = move-exception
            r6 = r1
            r1 = r0
            r0 = r6
        L47:
            java.lang.RuntimeException r2 = new java.lang.RuntimeException     // Catch: java.lang.Throwable -> L4d
            r2.<init>(r0)     // Catch: java.lang.Throwable -> L4d
            throw r2     // Catch: java.lang.Throwable -> L4d
        L4d:
            r0 = move-exception
        L4e:
            if (r1 == 0) goto L5b
            r1.close()     // Catch: java.io.IOException -> L54
            goto L5b
        L54:
            r0 = move-exception
            java.lang.RuntimeException r1 = new java.lang.RuntimeException
            r1.<init>(r0)
            throw r1
        L5b:
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.esri.android.map.FeatureLayer.getRenderer():com.esri.core.renderer.Renderer");
    }

    public void setRenderer(Renderer renderer) {
        try {
            this.v = renderer;
            if (this.nativeHandle != 0) {
                nativeSetRenderer(this.nativeHandle, renderer == null ? null : renderer.toJson());
            }
        } catch (Exception e2) {
            throw new RuntimeException(e2);
        }
    }

    public List<Feature> getSelectedFeatures() {
        ArrayList arrayList = new ArrayList();
        long[] selectionIDs = getSelectionIDs();
        if (selectionIDs != null) {
            for (long j : selectionIDs) {
                arrayList.add(getFeature(j));
            }
        }
        return arrayList;
    }

    public long[] getSelectionIDs() {
        if (this.nativeHandle != 0) {
            long[] nativeGetSelectionIDs = nativeGetSelectionIDs(this.nativeHandle);
            if (nativeGetSelectionIDs != null) {
                Arrays.sort(nativeGetSelectionIDs);
                return nativeGetSelectionIDs;
            }
            return nativeGetSelectionIDs;
        }
        return null;
    }

    public FeatureTable getFeatureTable() {
        return this.t;
    }

    public boolean isFeatureSelected(long j) {
        if (this.nativeHandle != 0) {
            return nativeIsFeatureSelected(this.nativeHandle, j);
        }
        return false;
    }

    public Future<FeatureResult> selectFeatures(QueryParameters queryParameters, final SelectionMode selectionMode, final CallbackListener<FeatureResult> callbackListener) {
        return this.t.queryFeatures(queryParameters, new CallbackListener<FeatureResult>() { // from class: com.esri.android.map.FeatureLayer.1
            @Override // com.esri.core.map.CallbackListener
            public void onError(Throwable th) {
                callbackListener.onError(th);
            }

            @Override // com.esri.core.map.CallbackListener
            /* renamed from: a */
            public void onCallback(FeatureResult featureResult) {
                if (featureResult != null) {
                    int i = AnonymousClass2.a[selectionMode.ordinal()];
                    if (i == 1) {
                        FeatureLayer.this.clearSelection();
                        FeatureLayer.this.b(featureResult);
                    } else if (i == 2) {
                        FeatureLayer.this.b(featureResult);
                    } else if (i == 3) {
                        FeatureLayer.this.unselectFeatures(featureResult);
                    }
                }
                callbackListener.onCallback(featureResult);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.esri.android.map.FeatureLayer$2  reason: invalid class name */
    /* loaded from: classes.dex */
    public static /* synthetic */ class AnonymousClass2 {
        static final /* synthetic */ int[] a;

        static {
            int[] iArr = new int[SelectionMode.values().length];
            a = iArr;
            try {
                iArr[SelectionMode.NEW.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                a[SelectionMode.ADD.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                a[SelectionMode.SUBTRACT.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void b(FeatureResult featureResult) {
        selectFeatures(a(featureResult), true);
    }

    public int getSelectionColor() {
        if (this.nativeHandle != 0) {
            return GraphicsUtil.abgrToColor(nativeGetSelectionColor(this.nativeHandle));
        }
        return 0;
    }

    public void setSelectionColorWidth(int i) {
        super.b(i);
    }

    public void setSelectionColor(int i) {
        if (this.nativeHandle == 0) {
            throw new IllegalStateException(b);
        }
        nativeSetSelectionColor(this.nativeHandle, GraphicsUtil.colorToAbgr(i));
    }

    @Override // com.esri.android.map.Layer
    public void recycle() {
        Future<Void> future = this.w;
        if (future != null) {
            future.cancel(true);
        }
        super.recycle();
    }

    @Override // com.esri.android.map.popup.PopupLayer
    public boolean isPopupEditable(Feature feature) {
        FeatureTable featureTable = this.t;
        if (featureTable instanceof GeodatabaseFeatureTable) {
            GeodatabaseFeatureTable geodatabaseFeatureTable = (GeodatabaseFeatureTable) featureTable;
            String capabilities = geodatabaseFeatureTable.getCapabilities();
            if (getPopupInfo(0) == null || capabilities == null || !capabilities.contains("Editing")) {
                return false;
            }
            if (capabilities.contains("Update") || !(capabilities.contains("Create") || capabilities.contains("Delete"))) {
                if (feature != null) {
                    return geodatabaseFeatureTable.canUpdate(feature.getId());
                }
                return true;
            }
            return false;
        }
        return false;
    }

    @Override // com.esri.android.map.popup.PopupLayer
    public boolean isPopupDeletable(Feature feature) {
        FeatureTable featureTable = this.t;
        if (featureTable instanceof GeodatabaseFeatureTable) {
            GeodatabaseFeatureTable geodatabaseFeatureTable = (GeodatabaseFeatureTable) featureTable;
            String capabilities = geodatabaseFeatureTable.getCapabilities();
            if (getPopupInfo(0) == null || capabilities == null || !capabilities.contains("Editing")) {
                return false;
            }
            if (capabilities.contains("Delete") || !(capabilities.contains("Create") || capabilities.contains("Update"))) {
                if (feature == null || geodatabaseFeatureTable.getOwnershipBasedAccessControlForFeatures() == null) {
                    return true;
                }
                return geodatabaseFeatureTable.canDelete(feature.getId());
            }
            return false;
        }
        return false;
    }

    @Override // com.esri.android.map.popup.PopupLayer
    public boolean isPopupAllowGeometryUpdate(Feature feature) {
        FeatureTable featureTable = this.t;
        if (featureTable instanceof GeodatabaseFeatureTable) {
            return ((GeodatabaseFeatureTable) featureTable).canUpdateGeometry();
        }
        return false;
    }

    public final void setDefinitionExpression(String str) {
        this.x = str;
        if (this.nativeHandle != 0) {
            nativeSetDefinitionExpression(this.nativeHandle, str);
        }
    }

    public String getDefinitionExpression() {
        return this.x;
    }

    protected void fetchFeatures(long[] jArr, double[] dArr, double d2) {
        FeatureTable featureTable = this.t;
        if (featureTable instanceof GeodatabaseFeatureServiceTable) {
            GeodatabaseFeatureServiceTable geodatabaseFeatureServiceTable = (GeodatabaseFeatureServiceTable) featureTable;
            Future<Void> future = this.w;
            if (future != null) {
                future.cancel(true);
            }
            this.w = geodatabaseFeatureServiceTable.fetchFeatures(jArr, dArr, d2);
        }
    }
}
